package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.*;
import com.itheima.common.Code;
import com.itheima.common.EmployeeConstant;
import com.itheima.common.MessageConstant;
import com.itheima.common.Result;
import com.itheima.domain.Employee;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName EmployeeController
 * @Author zhangwenjie
 * @Desc: 首页登录
 * @Date 12:28 2022/6/5
 * @Version 1.0
 **/
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    public EmployeeService employeeService;

    /**
     * 用户登录
     * @param employee
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody
                                Employee employee,
                        HttpServletRequest request,
                        HttpServletResponse response) {

        Employee login = employeeService.login(employee);
        //判断service层传入过来的是否为空
        if (login == null) {
            return new Result(Code.ERROR, MessageConstant.LOGIN_ERROR);
        }
        //往cookie存入用户信息令牌
        String toker = EmployeeConstant.EMPLOYEE_LOGIN + login.getId();
        Cookie cookie = new Cookie(EmployeeConstant.TOKEN, toker);
        //设置cookie的访问路径和时间
        cookie.setPath("/");
        cookie.setDomain("localhost");
        cookie.setMaxAge(60 * 30);
        response.addCookie(cookie);
        //把用户信息存入session
        request.getSession().setAttribute(toker, login.getId());
        //返回用户信息，用于前端的用户名回显
        return new Result(Code.OK, MessageConstant.LOGIN_SUCCESS, login);
    }

    /**
     * 用户登出
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request,
                         HttpServletResponse response){
        //找到应该删除的session的key
        String sid=null;
        HttpSession session = request.getSession();
        //session的key就是cookis的v所以找cookis就可以找到
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            //判断TOKEN和cookis的name是否相等,如果相等,就说明是咱们登录存入的cookis
            if (EmployeeConstant.TOKEN.equals(cookie.getName())){
                 sid=cookie.getValue();
            }
        }
        //删除session
        session.removeAttribute(sid);
        //删除cookis
        Cookie cookie = new Cookie(EmployeeConstant.TOKEN,"");
        cookie.setPath("/");
        cookie.setDomain("localhost");
        cookie.setMaxAge(EmployeeConstant.TOKEN_DEAD);
        response.addCookie(cookie);
        return new Result(Code.OK,MessageConstant.LOGOUT_SUCCESS);
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public PageResult page(Integer page, Integer pageSize, String name) {
        //当前页 和 每页条数 名字
        Page<Employee> pageResult = employeeService.page(page, pageSize, name);
        //1.当前页面记录列表
        List<Employee> records = pageResult.getRecords();
        //2.总记录数
        long total = pageResult.getTotal();
        return new PageResult(Code.OK, MessageConstant.EMP_SEARCH_SUCCESS, records, total);
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public Result employeeAdd(@RequestBody Employee employee) {
        //Long currentId = BaseContext.getCurrentId();
        int row = employeeService.empadd(employee);
        //判断
        if (row > 0) {
            return new Result(Code.OK, MessageConstant.EMP_SAVE_SUCCESS);
        }
        return new Result(Code.ERROR, MessageConstant.EMP_SAVE_ERROR);
    }

    /**
     * 根据员工信息编辑时回显数据
     *拼接的数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findByid(@PathVariable("id") Long id) {
        Employee employee = employeeService.findById(id);
        if (employee == null) {
            return new Result(Code.ERROR, MessageConstant.EMP_GET_ERROR);
        }
        //返回结果 并把查询到的 数据返回到页面
        return new Result(Code.OK, MessageConstant.EMP_GET_SUCCESS,employee);
    }

    /**
     * 员工启用禁用
     */
    @PutMapping
    public Result states(@RequestBody Employee employee,HttpServletRequest request){
        int row = employeeService.states(employee);
        if (row <1) {
            return new Result(Code.ERROR,MessageConstant.EMP_STATES_ERROR);
        }
        return new Result(Code.OK,MessageConstant.EMP_STATES_SUCCESS);
    }


}
